home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / kernel / raid / miscutil.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-19  |  7.2 KB  |  317 lines

  1. /* 
  2.  * miscutil.c --
  3.  *
  4.  * Copyright 1992 Regents of the University of California
  5.  * All rights reserved.
  6.  * Permission to use, copy, modify, and distribute this
  7.  * software and its documentation for any purpose and without
  8.  * fee is hereby granted, provided that the above copyright
  9.  * notice appear in all copies.  The University of California
  10.  * makes no representations about the suitability of this
  11.  * software for any purpose.  It is provided "as is" without
  12.  * express or implied warranty.
  13.  */
  14.  
  15. #ifndef lint
  16. static char rcsid[] = "$Header: /cdrom/src/kernel/Cvsroot/kernel/raid/miscutil.c,v 1.1 92/06/25 17:20:47 eklee Exp $ SPRITE (Berkeley)";
  17. #endif /* not lint */
  18.  
  19. #include "sync.h"
  20. #include "sprite.h"
  21. #include "fs.h"
  22. #include "dev.h"
  23. #include "devBlockDevice.h"
  24. #include "semaphore.h"
  25. #include "stdlib.h"
  26. #include "dbg.h"
  27. #include "miscutil.h"
  28. #include "varargs.h"
  29.  
  30.  
  31. /*
  32.  *----------------------------------------------------------------------
  33.  *
  34.  * Raid_InitSyncControl --
  35.  *
  36.  * Results:
  37.  *
  38.  * Side effects:
  39.  *
  40.  *----------------------------------------------------------------------
  41.  */
  42. void
  43. Raid_InitSyncControl(syncControlPtr)
  44.     SyncControl    *syncControlPtr;
  45. {
  46.     Sync_SemInitDynamic(&syncControlPtr->mutex, "SyncControl mtuex");
  47. #ifdef TESTING
  48.     Sync_CondInit(&syncControlPtr->wait);
  49. #endif TESTING
  50.     syncControlPtr->numIO = 1;
  51.     syncControlPtr->status = SUCCESS;
  52. }
  53.  
  54. /*
  55.  *----------------------------------------------------------------------
  56.  *
  57.  * Raid_StartIO --
  58.  *
  59.  * Results:
  60.  *
  61.  * Side effects:
  62.  *
  63.  *----------------------------------------------------------------------
  64.  */
  65. void
  66. Raid_StartSyncIO(syncControlPtr)
  67.     SyncControl    *syncControlPtr;
  68. {
  69.     MASTER_LOCK(&syncControlPtr->mutex);
  70.     syncControlPtr->numIO++;
  71.     MASTER_UNLOCK(&syncControlPtr->mutex);
  72. }
  73.  
  74. /*
  75.  *----------------------------------------------------------------------
  76.  *
  77.  * Raid_WaitIO --
  78.  *
  79.  * Results:
  80.  *
  81.  * Side effects:
  82.  *
  83.  *----------------------------------------------------------------------
  84.  */
  85. ReturnStatus
  86. Raid_WaitSyncIO(syncControlPtr)
  87.     SyncControl    *syncControlPtr;
  88. {
  89.     MASTER_LOCK(&syncControlPtr->mutex);
  90.     syncControlPtr->numIO--;
  91.     if (syncControlPtr->numIO == 0) {
  92.     MASTER_UNLOCK(&syncControlPtr->mutex);
  93.     } else {
  94.     Sync_MasterWait(&syncControlPtr->wait, &syncControlPtr->mutex, FALSE);
  95.     MASTER_UNLOCK(&syncControlPtr->mutex);
  96.     }
  97.     /*
  98.      * Setting numIO to 1 again allows syncControlPtr to be used again without
  99.      * calling Raid_InitSyncControl.
  100.      */
  101.     syncControlPtr->numIO = 1;
  102.     return syncControlPtr->status;
  103. }
  104.  
  105. /*
  106.  *----------------------------------------------------------------------
  107.  *
  108.  * Raid_SyncDoneProc --
  109.  *
  110.  * Results:
  111.  *
  112.  * Side effects:
  113.  *
  114.  *----------------------------------------------------------------------
  115.  */
  116. int
  117. Raid_SyncDoneProc(syncControlPtr, status)
  118.     SyncControl        *syncControlPtr;
  119.     ReturnStatus    status;
  120. {
  121.     if (status != SUCCESS) {
  122.         syncControlPtr->status = status;
  123.     }
  124.     MASTER_LOCK(&syncControlPtr->mutex);
  125.     syncControlPtr->numIO--;
  126.     if (syncControlPtr->numIO == 0) {
  127.         Sync_MasterBroadcast(&syncControlPtr->wait);
  128.     MASTER_UNLOCK(&syncControlPtr->mutex);
  129.     return 1;
  130.     } else {
  131.     MASTER_UNLOCK(&syncControlPtr->mutex);
  132.     return 0;
  133.     }
  134. }
  135.  
  136. /*
  137.  *----------------------------------------------------------------------
  138.  *
  139.  * DevIOSync --
  140.  *
  141.  * Results:
  142.  *
  143.  * Side effects:
  144.  *
  145.  *----------------------------------------------------------------------
  146.  */
  147. static ReturnStatus
  148. DevIOSync(devHandlePtr, devOffset, buffer, bufSize, op)
  149.     DevBlockDeviceHandle    *devHandlePtr;
  150.     int                devOffset;
  151.     char            *buffer;
  152.     int                bufSize;
  153.     int                op;
  154. {
  155.     DevBlockDeviceRequest       req;
  156.     int                         xferAmt;
  157.     ReturnStatus                status;
  158.  
  159.     req.operation       = op;
  160.     req.startAddress    = devOffset;
  161.     req.startAddrHigh   = 0;
  162.     req.buffer          = buffer;
  163.     req.bufferLen       = bufSize;
  164.     status = Dev_BlockDeviceIOSync(devHandlePtr, &req, &xferAmt);
  165.     if (xferAmt != bufSize) {
  166.     return FAILURE;
  167.     }
  168.     return status;
  169. }
  170.  
  171. /*
  172.  *----------------------------------------------------------------------
  173.  *
  174.  * Raid_DevReadSync --
  175.  *
  176.  * Results:
  177.  *
  178.  * Side effects:
  179.  *
  180.  *----------------------------------------------------------------------
  181.  */
  182. ReturnStatus
  183. Raid_DevReadSync(devHandlePtr, devOffset, buffer, bufSize)
  184.     DevBlockDeviceHandle    *devHandlePtr;
  185.     int                devOffset;
  186.     char            *buffer;
  187.     int                bufSize;
  188. {
  189.     return DevIOSync(devHandlePtr, devOffset, buffer, bufSize, FS_READ);
  190. }
  191.  
  192. /*
  193.  *----------------------------------------------------------------------
  194.  *
  195.  * Raid_DevWriteSync --
  196.  *
  197.  * Results:
  198.  *
  199.  * Side effects:
  200.  *
  201.  *----------------------------------------------------------------------
  202.  */
  203. ReturnStatus
  204. Raid_DevWriteSync(devHandlePtr, devOffset, buffer, bufSize)
  205.     DevBlockDeviceHandle    *devHandlePtr;
  206.     int                devOffset;
  207.     char            *buffer;
  208.     int                bufSize;
  209. {
  210.     return DevIOSync(devHandlePtr, devOffset, buffer, bufSize, FS_WRITE);
  211. }
  212.  
  213. /*
  214.  *----------------------------------------------------------------------
  215.  *
  216.  * Raid_DevWriteInt --
  217.  *
  218.  * Results:
  219.  *
  220.  * Side effects:
  221.  *
  222.  *----------------------------------------------------------------------
  223.  */
  224. /*VARARGS3*/
  225. ReturnStatus
  226. Raid_DevWriteInt(va_alist)
  227.     va_dcl
  228. {
  229.     DevBlockDeviceHandle        *devHandlePtr;
  230.     int                devOffset;
  231.     int                numInt;
  232.  
  233.     int            bufSize;
  234.     int            *buffer;
  235.     ReturnStatus        status;
  236.     int            i;
  237.     va_list        argList;
  238.  
  239.     va_start(argList);
  240.     devHandlePtr    = va_arg(argList, DevBlockDeviceHandle *);
  241.     devOffset        = va_arg(argList, int);
  242.     numInt        = va_arg(argList, int);
  243.  
  244.     bufSize = RoundUp(sizeof(int)*numInt, devHandlePtr->minTransferUnit);
  245.     buffer  = (int *) malloc(bufSize);
  246.     for (i = 0; i < numInt; i++) {
  247.     buffer[i] = va_arg(argList, int);
  248.     }
  249.     status = Raid_DevWriteSync(devHandlePtr, devOffset, (char *)buffer,bufSize);
  250.     free((char *) buffer);
  251.     return status;
  252. }
  253.  
  254. /*
  255.  *----------------------------------------------------------------------
  256.  *
  257.  * Raid_DevReadInt --
  258.  *
  259.  * Results:
  260.  *
  261.  * Side effects:
  262.  *
  263.  *----------------------------------------------------------------------
  264.  */
  265. /*VARARGS3*/
  266. ReturnStatus
  267. Raid_DevReadInt(va_alist)
  268.     va_dcl
  269. {
  270.     DevBlockDeviceHandle        *devHandlePtr;
  271.     int                devOffset;
  272.     int                numInt;
  273.  
  274.     int            bufSize;
  275.     int            *buffer;
  276.     ReturnStatus        status;
  277.     int            i;
  278.     va_list        argList;
  279.  
  280.     va_start(argList);
  281.     devHandlePtr    = va_arg(argList, DevBlockDeviceHandle *);
  282.     devOffset        = va_arg(argList, int);
  283.     numInt        = va_arg(argList, int);
  284.  
  285.     bufSize = RoundUp(sizeof(int *)*numInt, devHandlePtr->minTransferUnit);
  286.     buffer  = (int *) malloc(bufSize);
  287.     status = Raid_DevReadSync(devHandlePtr, devOffset, (char *)buffer, bufSize);
  288.     for (i = 0; i < numInt; i++) {
  289.     *va_arg(argList, int *) = buffer[i];
  290.     }
  291.     va_end(argList);
  292.     free((char *) buffer);
  293.     return status;
  294. }
  295.  
  296. /*
  297.  *----------------------------------------------------------------------
  298.  *
  299.  * Raid_WaitTime --
  300.  *
  301.  * Results:
  302.  *
  303.  * Side effects:
  304.  *
  305.  *----------------------------------------------------------------------
  306.  */
  307. void
  308. Raid_WaitTime(ms)
  309.     int        ms;
  310. {
  311.     Time time;
  312.  
  313.     time.seconds = ms/1000;
  314.     time.microseconds = (ms % 1000) * 1000;
  315.     Sync_WaitTime(time);
  316. }
  317.